package com.cbgolf.oa.manager;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

import android.content.Context;
import android.content.Intent;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.cbgolf.oa.activity.DialogActivity;
import com.cbgolf.oa.activity.message.MessageCenterActivity;
import com.cbgolf.oa.activity.order.OrderActivity;
import com.cbgolf.oa.activity.order.OrderDetailsActivity;
import com.cbgolf.oa.activity.publiz.LoginActivity;
import com.cbgolf.oa.app.App;
import com.cbgolf.oa.app.AppManager;
import com.cbgolf.oa.user.UserData;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;

/**
 * ReceiverManager
 *
 * @author lq
 */
public class ReceiverManager {

  private ReceiverManager() {

  }

  private static class Inner {

    private static ReceiverManager manager = new ReceiverManager();
  }

  public static ReceiverManager getInstance() {
    return Inner.manager;
  }

  public void showMessage(Context context, CPushMessage cPushMessage) {
    String title = cPushMessage.getTitle();
//    if (TextUtil.contains(title, "开球提醒")
//        || TextUtil.contains(title, "成绩发布")
//        || TextUtil.contains(title, "订场提醒")) {
    context.startActivity(new Intent(context, DialogActivity.class)
        .setFlags(FLAG_ACTIVITY_NEW_TASK)
        .putExtra("title", title)
        .putExtra("msg", cPushMessage.getContent()));
//    }
  }

  public void onNotificationOpened(Context context, String extraMap) {
    if (TextUtil.contains(extraMap, "订单消息通知")) {
      Class clazz = AppManager.getNewInstance().currentActivity().getClass();
      if (null == clazz) {
        return;
      }
      if (clazz.equals(OrderActivity.class) || clazz.equals(OrderDetailsActivity.class)) {
        return;
      }
      context.startActivity(new Intent(context, OrderActivity.class)
          .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
      return;
    }
    if (Util.isAppAlive(context, context.getPackageName())) {
      context.startActivity(new Intent(context, null == UserData.getUser() ? LoginActivity.class
          : MessageCenterActivity.class)
          .setFlags(FLAG_ACTIVITY_NEW_TASK));
    } else {
      Intent launchIntent = context.getPackageManager().
          getLaunchIntentForPackage(context.getPackageName());
      launchIntent.setFlags(
          FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
      context.startActivity(launchIntent);
    }


  }
  public void unBindAliAccount() {
    if (null != App.pushService) {
      App.pushService.unbindAccount(new CommonCallback() {
        @Override
        public void onSuccess(String s) {
          App.pushBindSuccess = false;
        }

        @Override
        public void onFailed(String s, String s1) {

        }
      });
    }
  }
  public void bindAliPush() {
    if (null != App.pushService && !Util.isNull(DataUtil.getUserId())) {
      App.pushService.bindAccount(DataUtil.getUserId(), new CommonCallback() {
        @Override
        public void onSuccess(String s) {
          App.pushBindSuccess = true;
        }

        @Override
        public void onFailed(String s, String s1) {
          App.pushBindSuccess = false;
        }
      });
    }
  }

  public void closeWebSocket() {
    try {
      WsStompManager.getInstance().disconnect();
    } catch (Exception e) {

    }
  }
}
